package github.flandre.examplemod.common.item;

import github.flandre.examplemod.common.mycapability.ISpeedUpCapability;
import github.flandre.examplemod.core.init.EffectInit;
import github.flandre.examplemod.core.init.Itemgroup.ExampleGroup;
import github.flandre.examplemod.core.registry.CommonSetupEventHandler;
import github.flandre.examplemod.network.NetworkRegistryHandler;
import github.flandre.examplemod.network.Packet.Power;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.entity.projectile.SnowballEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.*;
import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.network.PacketDistributor;

public class InfiniteSnowball extends Item {
    public InfiniteSnowball() {
        super(new Properties().group(ExampleGroup.exampleGroup));
    }


    @Override //当前世界 当前玩家 持有位置
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {

        //获取手上的物品
        ItemStack heldItem = playerIn.getHeldItem(handIn);
        playerIn.addPotionEffect(
                new EffectInstance(
                        EffectInit.EXAMPLE_EFFECT.get(),
                        9800)//时长
                //可以传递多个药水效果
        );
        //播放声音
        //让操作位于服务端
        if(!worldIn.isRemote)
        {

            LazyOptional<ISpeedUpCapability> capability = playerIn.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
            capability.ifPresent((c)->{
                if(c.getLevel() >= 4)
                {
                    worldIn.playSound(null,playerIn.getPosX(),playerIn.getPosY(),playerIn.getPosZ(), SoundEvents.ENTITY_SNOWBALL_THROW, SoundCategory.NEUTRAL,0.5F,0.4F /(random.nextFloat() * 0.4F + 0.8F));
                    //创建一个雪球s
                    SnowballEntity snowballEntity = new SnowballEntity(worldIn, playerIn);
                    //配置雪球
                    snowballEntity.func_234612_a_(playerIn,
                            playerIn.rotationPitch,
                            playerIn.rotationYaw,
                            0.0f,1.5f,1.0f
                    );
                    //产生实体
                    worldIn.addEntity(snowballEntity);
                    //如果玩家不是创造的话物品-1
                    //            if(!playerIn.abilities.isCreativeMode)
                    //            {
                    //                heldItem.shrink(1);
                    //            }

                    c.setLevel(c.getLevel()-4);
                    //告诉客户端同步数据
                    NetworkRegistryHandler.CHANNEL.send(PacketDistributor.PLAYER.with(()->(ServerPlayerEntity) playerIn),new Power(c.getLevel()));
                }
                else
                {
                    worldIn.playSound(null,playerIn.getPosX(),playerIn.getPosY(),playerIn.getPosZ(), SoundEvents.ENTITY_VILLAGER_NO, SoundCategory.NEUTRAL,0.5F,0.4F /(random.nextFloat() * 0.4F + 0.8F));
                }
            });


        }

        //返回成功执行
        return new ActionResult<>(ActionResultType.SUCCESS,heldItem);
        //return super.onItemRightClick(worldIn, playerIn, handIn);
    }
}
